import 'package:enjoy_plus/pages/house/building_list.dart';
import 'package:enjoy_plus/pages/house/house_detail.dart';
import 'package:enjoy_plus/pages/house/house_form.dart';
import 'package:enjoy_plus/pages/house/house_list.dart';
import 'package:enjoy_plus/pages/house/location_list.dart';
import 'package:enjoy_plus/pages/house/room_list.dart';
import 'package:enjoy_plus/pages/login/index.dart';
import 'package:enjoy_plus/pages/notice/index.dart';
import 'package:enjoy_plus/pages/profile/index.dart';
import 'package:enjoy_plus/pages/tab_bar_page.dart';
import 'package:enjoy_plus/utils/token.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      routes: {
        '/': (context) => const TabBarPage(),
        '/detail': (context) => const NoticeDetail(),
        '/login': (context) => const LoginPage(),
        // '/profile': (context) => const ProfilePage(),
      },

      //   initialRoute: '/',
      //   // 动态生成路由
      //   onGenerateRoute: (settings) {
      //     // 获取 token
      //     final token = TokenManager().getToken();
      //     // 如果 token 为空，跳转到登录页
      //     if (token == null) {
      //       return MaterialPageRoute(builder: (context) => const LoginPage());
      //     } else if (settings.name == '/profile') {
      //       return MaterialPageRoute(builder: (context) => const ProfilePage());
      //     }

      //     return null;
      //   },
      // ));
      onGenerateRoute: (settings) {
        final tokenManager = TokenManager();
        final token = tokenManager.getToken() ?? '';
        if (token.isEmpty && settings.name != '/login') {
          return MaterialPageRoute(builder: (context) => const LoginPage());
        }

        if (settings.name == '/profile') {
          return MaterialPageRoute(
            builder:
                (context) => ProfilePage(userInfo: settings.arguments as Map),
          );
        }

        if (settings.name == '/house_list') {
          return MaterialPageRoute(builder: (context) => const HouseList());
        } else if (settings.name == '/location_list') {
          return MaterialPageRoute(builder: (context) => const LocationList());
        }

        // 分步表单2: 选择楼栋
        if (settings.name == '/building_list') {
          Map params = settings.arguments as Map;
          return MaterialPageRoute(
            builder:
                (context) => BuildingList(
                  point: params['point'], // 传递小区信息
                ),
          );
        }

        // 分步表单3: 选择房间
        if (settings.name == '/room_list') {
          Map params = settings.arguments as Map;
          return MaterialPageRoute(
            builder:
                (context) => RoomList(
                  point: params['point'], // 传递小区信息
                  building: params['building'], // 传递楼栋信息
                ),
          );
        }

        if (settings.name == '/house_form') {
          return MaterialPageRoute(
            builder: (context) => HouseForm(params: settings.arguments as Map),
          );
        }

        if (settings.name == '/house_detail') {
          return MaterialPageRoute(
            builder:
                (context) => HouseDetail(id: (settings.arguments as Map)['id']),
          );
        }

        return null;
      },
      initialRoute: '/',
    ),
  );
}
